/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PnlCampaign.java
 *
 * Created on May 2, 2010, 9:47:26 PM
 */
package com.aptech.campaign.ui;

import com.aptech.campaign.biz.CampaignBiz;
import com.aptech.campaign.component.ColumnData;
import com.aptech.campaign.component.ObjectTableModel;
import com.aptech.campaign.entities.Campaign;
import com.aptech.campaign.entities.Product;
import com.aptech.campaign.entities.TypeCampaign;
import com.aptech.campaign.entities.TypeOffer;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JViewport;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;

/**
 *
 * @author Tai Nguyen
 */
public class CampaignPnl extends javax.swing.JPanel {

    CampaignMainFrm main;
    CampaignBiz camBuz = new CampaignBiz();
    public ObjectTableModel tableModel;
    public ArrayList<Product> arr = null;
    public JTable headerTable;
    ArrayList<Campaign> arrCam = null;
    ArrayList<Product> arrPro = null;
    ArrayList<TypeCampaign> arrTC = null;
    ArrayList<TypeOffer> arrTO = null;

    /** Creates new form PnlCampaign */
    public CampaignPnl() {
        initComponents();
        loadTable();
        putDataForCbb();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    /**
     * validate data on fields
     * @param
     * @return boolean value
     */
    private boolean valid() {
        if (!txfName.getText().equalsIgnoreCase("") && cbbProduct.getSelectedIndex() != 0 && cbbTypeCam.getSelectedIndex() != 0
                && dtcEndDate.getDate() != null && dtcStartDate.getDate() != null) {
            return true;
        }
        return false;
    }

    /**
     * 
     */
    private void reset() {
        txfName.setText("");
        cbbProduct.setSelectedIndex(0);
        cbbTypeCam.setSelectedIndex(0);
        cbbTypeOffer.setSelectedIndex(0);
        dtcEndDate.setDate(null);
        dtcStartDate.setDate(null);
    }

    /**
     * get Campaign object from data on fields
     * @param
     * @return Campaign cam
     */
    private Campaign getObjCam() {
        //get ProductID of product selected
        int indexCbbPro = cbbProduct.getSelectedIndex() - 1;
        int proID = arrPro.get(indexCbbPro).getProductID();

        //get TypeID of TypeCampaign selected
        int indexCbbCam = cbbTypeCam.getSelectedIndex() - 1;
        int typeID = arrTC.get(indexCbbCam).getTypeID();

        //get OfferID of Offer selected
        int indexCbbOff = cbbTypeOffer.getSelectedIndex() - 1;
        int offerID;
        if (indexCbbOff >= 0) {
            offerID = arrTO.get(indexCbbOff).getOfferID();
        } else {
            offerID = 0;
        }


        //convert date calendar to date sql
        Calendar caStart = Calendar.getInstance();
        caStart.setTime(dtcStartDate.getDate());
        java.sql.Date startDate = new java.sql.Date(caStart.getTimeInMillis());

        Calendar caEnd = Calendar.getInstance();
        caEnd.setTime(dtcEndDate.getDate());
        java.sql.Date endDate = new java.sql.Date(caEnd.getTimeInMillis());

        //set object
        Campaign cam = new Campaign();
        cam.setCampaignName(txfName.getText());
        cam.setProductID(proID);
        cam.setStartDate(startDate);
        cam.setEndDate(endDate);
        cam.setTypeID(typeID);
        cam.setOfferID(offerID);

        return cam;
    }
    /*
     * 
     */

    private void putDataForCbb() {
        arrPro = camBuz.findByAllProduct();
        arrTC = camBuz.findByAllTypeCampaign();
        arrTO = camBuz.findByAllTypeOffer();

        //add item into cbbProdcut
        cbbProduct.removeAllItems();
        cbbProduct.addItem("--Select Product--");
        for (int i = 0; i < arrPro.size(); i++) {
            String item = "ID:" + arrPro.get(i).getProductID() + "-" + arrPro.get(i).getProductName() + "-" + arrPro.get(i).getPrice() + "$";
            cbbProduct.addItem(item);
        }
        //add item into cbbTypeCam
        cbbTypeCam.removeAllItems();
        cbbTypeCam.addItem("--Select TypeCampaign--");
        for (int i = 0; i < arrTC.size(); i++) {
            String item = ("ID:" + arrTC.get(i).getTypeID() + "-" + arrTC.get(i).getTypeName() + "-" + arrTC.get(i).getCostForDay() + "$");
            cbbTypeCam.addItem(item);
        }

        //add item into cbbTypeOffer
        cbbTypeOffer.removeAllItems();
        cbbTypeOffer.addItem("--Select Offer--");
        for (int i = 0; i < arrTO.size(); i++) {
            String item = ("ID:" + arrTO.get(i).getOfferID() + "-" + arrTO.get(i).getOfferName() + "-" + arrTO.get(i).getCost() + "$");
            cbbTypeOffer.addItem(item);
        }
    }
    /*
     * 
     */

    public void loadTable() {
        arrCam = camBuz.findByAllCampaign();
        ColumnData[] columns = {
            new ColumnData("Name", 150, SwingConstants.LEFT, 2),
            new ColumnData("ProductID", 50, SwingConstants.LEFT, 3),
            new ColumnData("StartDate", 100, SwingConstants.LEFT, 4),
            new ColumnData("EndDate", 100, SwingConstants.LEFT, 5),
            new ColumnData("TypeID", 30, SwingConstants.LEFT, 6),
            new ColumnData("OfferID", 30, SwingConstants.LEFT, 7)
        };

        tableModel = new ObjectTableModel(tbCampaign, columns, arrCam);
        headerTable = tableModel.getHeaderTable();

        //Auto set ordinal
        headerTable.createDefaultColumnsFromModel();
        tbCampaign.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

        //Put it in a viewport that we can control a bit
        JViewport viewport = new JViewport();

        //display data with ordinal
        viewport.setView(headerTable);
        viewport.setPreferredSize(headerTable.getMaximumSize());

        scrCampaign.setRowHeader(viewport);
        //scrProduct.setPreferredSize(new Dimension(350, 300));
        scrCampaign.setCorner(ScrollPaneConstants.UPPER_LEFT_CORNER, headerTable.getTableHeader());
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        scrCampaign = new javax.swing.JScrollPane();
        tbCampaign = new javax.swing.JTable();
        pnlEdit = new javax.swing.JPanel();
        btnEdit = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        pnlNewCam = new javax.swing.JPanel();
        lblName = new javax.swing.JLabel();
        lblProDuct = new javax.swing.JLabel();
        lblStart = new javax.swing.JLabel();
        txfName = new javax.swing.JTextField();
        btnReset = new javax.swing.JButton();
        btnAdd = new javax.swing.JButton();
        lblEnd = new javax.swing.JLabel();
        lblTypeCam = new javax.swing.JLabel();
        lblTypeOffer = new javax.swing.JLabel();
        cbbProduct = new javax.swing.JComboBox();
        cbbTypeCam = new javax.swing.JComboBox();
        cbbTypeOffer = new javax.swing.JComboBox();
        dtcStartDate = new com.toedter.calendar.JDateChooser();
        dtcEndDate = new com.toedter.calendar.JDateChooser();
        pnlTypeCam = new javax.swing.JPanel();
        lblTypeName = new javax.swing.JLabel();
        txfTypeName = new javax.swing.JTextField();
        lblCoasforDay = new javax.swing.JLabel();
        txfCostForDay = new javax.swing.JTextField();
        btnAddType = new javax.swing.JButton();
        btnCancelType = new javax.swing.JButton();
        pnlTypeOffer = new javax.swing.JPanel();
        btnCancelOffer = new javax.swing.JButton();
        btnAddTypeOffer = new javax.swing.JButton();
        lblCost = new javax.swing.JLabel();
        lblNameOffer = new javax.swing.JLabel();
        txfCost = new javax.swing.JTextField();
        txfNameOffer = new javax.swing.JTextField();
        pnlTitle = new javax.swing.JPanel();
        lblTitle = new javax.swing.JLabel();

        setMinimumSize(new java.awt.Dimension(100, 100));
        setPreferredSize(new java.awt.Dimension(100, 100));
        setLayout(new java.awt.GridBagLayout());

        scrCampaign.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "CAMPAIGN", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 14), new java.awt.Color(51, 102, 255))); // NOI18N

        tbCampaign.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        tbCampaign.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        tbCampaign.setIntercellSpacing(new java.awt.Dimension(5, 1));
        scrCampaign.setViewportView(tbCampaign);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 557;
        gridBagConstraints.ipady = 122;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 0.55;
        add(scrCampaign, gridBagConstraints);

        pnlEdit.setMaximumSize(new java.awt.Dimension(200, 70));
        pnlEdit.setMinimumSize(new java.awt.Dimension(100, 20));
        pnlEdit.setPreferredSize(new java.awt.Dimension(100, 20));
        pnlEdit.setLayout(new java.awt.GridBagLayout());

        btnEdit.setBackground(new java.awt.Color(51, 102, 255));
        btnEdit.setFont(new java.awt.Font("Tahoma", 1, 14));
        btnEdit.setForeground(new java.awt.Color(255, 255, 255));
        btnEdit.setText("Edit");
        btnEdit.setMaximumSize(new java.awt.Dimension(59, 20));
        btnEdit.setMinimumSize(new java.awt.Dimension(59, 20));
        btnEdit.setPreferredSize(new java.awt.Dimension(59, 20));
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 9);
        pnlEdit.add(btnEdit, gridBagConstraints);

        btnDelete.setBackground(new java.awt.Color(51, 102, 255));
        btnDelete.setFont(new java.awt.Font("Tahoma", 1, 14));
        btnDelete.setForeground(new java.awt.Color(255, 255, 255));
        btnDelete.setText("Delete");
        btnDelete.setMaximumSize(new java.awt.Dimension(75, 20));
        btnDelete.setMinimumSize(new java.awt.Dimension(75, 20));
        btnDelete.setPreferredSize(new java.awt.Dimension(75, 20));
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        pnlEdit.add(btnDelete, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 194;
        gridBagConstraints.ipady = 23;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 0.4;
        gridBagConstraints.weighty = 0.05;
        add(pnlEdit, gridBagConstraints);

        pnlNewCam.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "NEW CAMPAIGN", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 14), new java.awt.Color(51, 102, 255))); // NOI18N
        pnlNewCam.setMaximumSize(new java.awt.Dimension(200, 200));
        pnlNewCam.setMinimumSize(new java.awt.Dimension(200, 200));
        pnlNewCam.setPreferredSize(new java.awt.Dimension(261, 200));
        pnlNewCam.setLayout(new java.awt.GridBagLayout());

        lblName.setText("Campaign Name : ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 13, 0);
        pnlNewCam.add(lblName, gridBagConstraints);

        lblProDuct.setText("Product : ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 13, 0);
        pnlNewCam.add(lblProDuct, gridBagConstraints);

        lblStart.setText("Start Date : ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 13, 0);
        pnlNewCam.add(lblStart, gridBagConstraints);

        txfName.setColumns(15);
        txfName.setMaximumSize(new java.awt.Dimension(250, 20));
        txfName.setMinimumSize(new java.awt.Dimension(150, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 10, 13, 5);
        pnlNewCam.add(txfName, gridBagConstraints);

        btnReset.setBackground(new java.awt.Color(51, 102, 255));
        btnReset.setFont(new java.awt.Font("Tahoma", 1, 14));
        btnReset.setForeground(new java.awt.Color(255, 255, 255));
        btnReset.setText("Reset");
        btnReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnResetActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        pnlNewCam.add(btnReset, gridBagConstraints);

        btnAdd.setBackground(new java.awt.Color(51, 102, 255));
        btnAdd.setFont(new java.awt.Font("Tahoma", 1, 14));
        btnAdd.setForeground(new java.awt.Color(255, 255, 255));
        btnAdd.setText("Add");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 0);
        pnlNewCam.add(btnAdd, gridBagConstraints);

        lblEnd.setText("End Date : ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 13, 0);
        pnlNewCam.add(lblEnd, gridBagConstraints);

        lblTypeCam.setText("Type Campaign : ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 12, 0);
        pnlNewCam.add(lblTypeCam, gridBagConstraints);

        lblTypeOffer.setText("Type Offer : ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 12, 0);
        pnlNewCam.add(lblTypeOffer, gridBagConstraints);

        cbbProduct.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "--Select Product--" }));
        cbbProduct.setMaximumSize(new java.awt.Dimension(250, 20));
        cbbProduct.setMinimumSize(new java.awt.Dimension(170, 20));
        cbbProduct.setPreferredSize(new java.awt.Dimension(150, 20));
        cbbProduct.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbbProductActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 10, 13, 0);
        pnlNewCam.add(cbbProduct, gridBagConstraints);

        cbbTypeCam.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "--Select Type Campaign--" }));
        cbbTypeCam.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbbTypeCamActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 10, 12, 5);
        pnlNewCam.add(cbbTypeCam, gridBagConstraints);

        cbbTypeOffer.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "--Select Type Offer--" }));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 10, 12, 0);
        pnlNewCam.add(cbbTypeOffer, gridBagConstraints);

        dtcStartDate.setMinimumSize(new java.awt.Dimension(150, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 10, 13, 5);
        pnlNewCam.add(dtcStartDate, gridBagConstraints);

        dtcEndDate.setMinimumSize(new java.awt.Dimension(150, 20));
        dtcEndDate.setPreferredSize(new java.awt.Dimension(100, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 10, 13, 5);
        pnlNewCam.add(dtcEndDate, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridheight = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 96;
        gridBagConstraints.ipady = 108;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 0.6;
        gridBagConstraints.weighty = 0.35;
        add(pnlNewCam, gridBagConstraints);

        pnlTypeCam.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "TYPE CAMPAIGN", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 14), new java.awt.Color(51, 102, 255))); // NOI18N
        pnlTypeCam.setLayout(new java.awt.GridBagLayout());

        lblTypeName.setText("Name:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 9, 0);
        pnlTypeCam.add(lblTypeName, gridBagConstraints);

        txfTypeName.setColumns(10);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.ipadx = 53;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 9, 9, 0);
        pnlTypeCam.add(txfTypeName, gridBagConstraints);

        lblCoasforDay.setText("CostForDay:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 9, 0);
        pnlTypeCam.add(lblCoasforDay, gridBagConstraints);

        txfCostForDay.setColumns(10);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.ipadx = 53;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 9, 9, 0);
        pnlTypeCam.add(txfCostForDay, gridBagConstraints);

        btnAddType.setBackground(new java.awt.Color(51, 102, 255));
        btnAddType.setFont(new java.awt.Font("Tahoma", 1, 11));
        btnAddType.setForeground(new java.awt.Color(255, 255, 255));
        btnAddType.setText("Add");
        btnAddType.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddTypeActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        pnlTypeCam.add(btnAddType, gridBagConstraints);

        btnCancelType.setBackground(new java.awt.Color(51, 102, 255));
        btnCancelType.setFont(new java.awt.Font("Tahoma", 1, 11));
        btnCancelType.setForeground(new java.awt.Color(255, 255, 255));
        btnCancelType.setText("Cancel");
        btnCancelType.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelTypeActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 9, 0, 0);
        pnlTypeCam.add(btnCancelType, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 147;
        gridBagConstraints.ipady = 22;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 0.4;
        gridBagConstraints.weighty = 0.15;
        add(pnlTypeCam, gridBagConstraints);

        pnlTypeOffer.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "TYPE OFFER", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 14), new java.awt.Color(51, 102, 255))); // NOI18N
        pnlTypeOffer.setLayout(new java.awt.GridBagLayout());

        btnCancelOffer.setBackground(new java.awt.Color(51, 102, 255));
        btnCancelOffer.setFont(new java.awt.Font("Tahoma", 1, 11));
        btnCancelOffer.setForeground(new java.awt.Color(255, 255, 255));
        btnCancelOffer.setText("Cancel");
        btnCancelOffer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelOfferActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 9, 0, 0);
        pnlTypeOffer.add(btnCancelOffer, gridBagConstraints);

        btnAddTypeOffer.setBackground(new java.awt.Color(51, 102, 255));
        btnAddTypeOffer.setFont(new java.awt.Font("Tahoma", 1, 11));
        btnAddTypeOffer.setForeground(new java.awt.Color(255, 255, 255));
        btnAddTypeOffer.setText("Add");
        btnAddTypeOffer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddTypeOfferActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        pnlTypeOffer.add(btnAddTypeOffer, gridBagConstraints);

        lblCost.setText("Cost:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 9, 0);
        pnlTypeOffer.add(lblCost, gridBagConstraints);

        lblNameOffer.setText("Name:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 9, 0);
        pnlTypeOffer.add(lblNameOffer, gridBagConstraints);

        txfCost.setColumns(10);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.ipadx = 53;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 9, 9, 0);
        pnlTypeOffer.add(txfCost, gridBagConstraints);

        txfNameOffer.setColumns(10);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.ipadx = 53;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 9, 9, 0);
        pnlTypeOffer.add(txfNameOffer, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 157;
        gridBagConstraints.ipady = 23;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 0.4;
        gridBagConstraints.weighty = 0.15;
        add(pnlTypeOffer, gridBagConstraints);

        pnlTitle.setBackground(new java.awt.Color(51, 102, 255));
        pnlTitle.setForeground(new java.awt.Color(51, 102, 255));
        pnlTitle.setMinimumSize(new java.awt.Dimension(252, 50));
        pnlTitle.setPreferredSize(new java.awt.Dimension(252, 37));
        pnlTitle.setLayout(new java.awt.BorderLayout());

        lblTitle.setBackground(new java.awt.Color(51, 102, 255));
        lblTitle.setFont(new java.awt.Font("Tahoma", 1, 30));
        lblTitle.setForeground(new java.awt.Color(102, 102, 0));
        lblTitle.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/aptech/campaign/icon/campaignbanner.jpg"))); // NOI18N
        lblTitle.setMinimumSize(new java.awt.Dimension(652, 30));
        lblTitle.setPreferredSize(new java.awt.Dimension(652, 30));
        pnlTitle.add(lblTitle, java.awt.BorderLayout.CENTER);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 338;
        gridBagConstraints.ipady = -8;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 0.1;
        add(pnlTitle, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    private void cbbProductActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbbProductActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_cbbProductActionPerformed

    private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditActionPerformed
        //get index of the row is selected on table
        int indexRow = tbCampaign.getSelectedRow();
        if (indexRow != -1) {
            EditCampaignDlg cam = new EditCampaignDlg(this, true);
            //
            arrCam = camBuz.findByAllCampaign();
            //set info on fields of EditCampaignDlg
            cam.txfCampaignID.setText("" + arrCam.get(indexRow).getCampaignID());
            cam.txfName.setText(arrCam.get(indexRow).getCampaignName());
            arrPro = camBuz.findByAllProduct();
            //add item into cbbProdcut
            for (int i = 0; i < arrPro.size(); i++) {
                String item = "ID:" + arrPro.get(i).getProductID() + "-" + arrPro.get(i).getProductName() + "-" + arrPro.get(i).getPrice() + "$";
                cam.cbbProduct.insertItemAt(item, i + 1);
            }
            //cam.cbbProduct.setSelectedIndex(arrCam.get(indexRow).getProductID());
            arrTC = camBuz.findByAllTypeCampaign();
            //add item into cbbTypeCam
            for (int i = 0; i < arrTC.size(); i++) {
                String item = ("ID:" + arrTC.get(i).getTypeID() + "-" + arrTC.get(i).getTypeName() + "-" + arrTC.get(i).getCostForDay() + "$");
                cam.cbbTypecam.insertItemAt(item, i + 1);
            }
            arrTO = camBuz.findByAllTypeOffer();
            //add item into cbbTypeOffer
            for (int i = 0; i < arrTO.size(); i++) {
                String item = ("ID:" + arrTO.get(i).getOfferID() + "-" + arrTO.get(i).getOfferName() + "-" + arrTO.get(i).getCost() + "$");
                cam.cbbTypeOffer.insertItemAt(item, i + 1);
            }
            cam.dtcStartDate.setDate(arrCam.get(indexRow).getStartDate());
            cam.dtcEbdDate.setDate(arrCam.get(indexRow).getEndDate());

            cam.setSize(400, 500);
            cam.setVisible(true);
        } else {
            JOptionPane.showMessageDialog(this, "Please select Campaign!");
        }

    }//GEN-LAST:event_btnEditActionPerformed

    private void btnResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnResetActionPerformed
        reset();
    }//GEN-LAST:event_btnResetActionPerformed

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        if (!valid()) {
            JOptionPane.showMessageDialog(this, "Please input data fully!");
        } else {
            Campaign cam = getObjCam();
            if (!cam.validate()) {
                JOptionPane.showMessageDialog(this, cam.getLastError());
            } else if (camBuz.createCampaign(getObjCam())) {
                JOptionPane.showMessageDialog(this, camBuz.getLasterror());
                loadTable();
                reset();
            } else {
                JOptionPane.showMessageDialog(this, camBuz.getLasterror());
            }
        }
    }//GEN-LAST:event_btnAddActionPerformed

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
        //get index of the row is selected on table
        int indexRow = tbCampaign.getSelectedRow();
        if (indexRow != -1) {
            arrCam = camBuz.findByAllCampaign();
            int camID = arrCam.get(indexRow).getCampaignID();
            int opt = JOptionPane.showConfirmDialog(this, "Delete this Campaign?", "Delete Campaign", JOptionPane.YES_NO_OPTION);
            if (opt == 0) {
                if (camBuz.removeCampaign(camID)) {
                    JOptionPane.showMessageDialog(this, camBuz.getLasterror());
                    loadTable();
                } else {
                    JOptionPane.showMessageDialog(this, camBuz.getLasterror());
                }
            }
        } else {
            JOptionPane.showMessageDialog(this, "Please select Campaign!");
        }

    }//GEN-LAST:event_btnDeleteActionPerformed

    private void btnAddTypeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddTypeActionPerformed
        if (txfTypeName.getText().equalsIgnoreCase("") || txfCostForDay.getText().equalsIgnoreCase("")) {
            JOptionPane.showMessageDialog(this, "Please input data Fully!");
        } else {
            Pattern pattern = Pattern.compile("[0-9]+[.]?[0-9]+");
            Matcher m1 = pattern.matcher(txfCostForDay.getText());
            if (!m1.matches()) {
                JOptionPane.showMessageDialog(this, "Please enter numeric value for CostForDay!");
            } else {
                TypeCampaign tc = new TypeCampaign();
                tc.setTypeName(txfTypeName.getText());
                tc.setCostForDay(Float.parseFloat(txfCostForDay.getText()));
                if (camBuz.createTypeCampaign(tc)) {
                    JOptionPane.showMessageDialog(this, camBuz.getLasterror());
                    txfTypeName.setText("");
                    txfCostForDay.setText("");
                    putDataForCbb();
                } else {
                    JOptionPane.showMessageDialog(this, camBuz.getLasterror());
                }
            }
        }
    }//GEN-LAST:event_btnAddTypeActionPerformed

    private void cbbTypeCamActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbbTypeCamActionPerformed
    }//GEN-LAST:event_cbbTypeCamActionPerformed

    private void btnCancelTypeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelTypeActionPerformed
        txfTypeName.setText("");
        txfCostForDay.setText("");
    }//GEN-LAST:event_btnCancelTypeActionPerformed

    private void btnAddTypeOfferActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddTypeOfferActionPerformed
        if (txfNameOffer.getText().equalsIgnoreCase("") || txfCost.getText().equalsIgnoreCase("")) {
            JOptionPane.showMessageDialog(this, "Please input data Fully!");
        } else {
            Pattern pattern = Pattern.compile("[0-9]+[.]?[0-9]+");
            Matcher m1 = pattern.matcher(txfCost.getText());
            if (!m1.matches()) {
                JOptionPane.showMessageDialog(this, "Please enter numeric value for Cost!");
            } else {
                TypeOffer to = new TypeOffer();
                to.setOfferName(txfNameOffer.getText());
                to.setCost(Float.parseFloat(txfCost.getText()));
                if (camBuz.createTypeOffer(to)) {
                    JOptionPane.showMessageDialog(this, camBuz.getLasterror());
                    txfNameOffer.setText("");
                    txfCost.setText("");
                    putDataForCbb();
                } else {
                    JOptionPane.showMessageDialog(this, camBuz.getLasterror());
                }
            }
        }
    }//GEN-LAST:event_btnAddTypeOfferActionPerformed

    private void btnCancelOfferActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelOfferActionPerformed
        txfNameOffer.setText("");
        txfCost.setText("");
    }//GEN-LAST:event_btnCancelOfferActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnAddType;
    private javax.swing.JButton btnAddTypeOffer;
    private javax.swing.JButton btnCancelOffer;
    private javax.swing.JButton btnCancelType;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnEdit;
    private javax.swing.JButton btnReset;
    private javax.swing.JComboBox cbbProduct;
    private javax.swing.JComboBox cbbTypeCam;
    private javax.swing.JComboBox cbbTypeOffer;
    private com.toedter.calendar.JDateChooser dtcEndDate;
    private com.toedter.calendar.JDateChooser dtcStartDate;
    private javax.swing.JLabel lblCoasforDay;
    private javax.swing.JLabel lblCost;
    private javax.swing.JLabel lblEnd;
    private javax.swing.JLabel lblName;
    private javax.swing.JLabel lblNameOffer;
    private javax.swing.JLabel lblProDuct;
    private javax.swing.JLabel lblStart;
    private javax.swing.JLabel lblTitle;
    private javax.swing.JLabel lblTypeCam;
    private javax.swing.JLabel lblTypeName;
    private javax.swing.JLabel lblTypeOffer;
    private javax.swing.JPanel pnlEdit;
    private javax.swing.JPanel pnlNewCam;
    private javax.swing.JPanel pnlTitle;
    private javax.swing.JPanel pnlTypeCam;
    private javax.swing.JPanel pnlTypeOffer;
    private javax.swing.JScrollPane scrCampaign;
    private javax.swing.JTable tbCampaign;
    private javax.swing.JTextField txfCost;
    private javax.swing.JTextField txfCostForDay;
    private javax.swing.JTextField txfName;
    private javax.swing.JTextField txfNameOffer;
    private javax.swing.JTextField txfTypeName;
    // End of variables declaration//GEN-END:variables
}
